package com.rectcircle.wechat.util;

import java.io.IOException;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 
 * 对消息的处理类目前JSON文本
 * @author RectCircle
 * @version 0.1<br>
 * 2016年7月13日-下午6:22:00<br>
 * Copyright (c) 2016 RectCircle-版权所有<br>
 *
 */
public class JsonHandler {
	
	private static ObjectMapper objectMapper = new ObjectMapper();
	
	public JsonHandler(){
	}
	
	public static <T> T parseJson(String jsonString, Class<T> clazz){
		try {
			return objectMapper.readValue(jsonString, clazz);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//TODO 未测试修改include模式
	public static String ctreateJson(Object obj){
		
		objectMapper.setSerializationInclusion(Include.NON_NULL);
		
		String result = null;
		
		try {
			result = objectMapper.writeValueAsString(obj);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		} finally {			
			objectMapper.setSerializationInclusion(Include.ALWAYS);		
		}
		
		return result;
	}
	
}
